// others betteranswer
// function squareDigits(num) {
//     return Number(('' + num)
//             .split('')
//             .map(function(val) {
//                 return val * val;
//             })
//             .join('')
//     );
// }

function squareDigits(num) {
    var arr = (num + '').split('');
    var newArr = arr
        .map(function(item, index) {
            return parseInt(item, 10) * parseInt(item, 10) + '';
        })
        .toString()
        .replace(/,/g, '');

    return newArr;
}
squareDigits(9119);

function findSum(n) {
    var arr = [];
    var sum = 0;
    while (n > 1) {
        if (n % 3 == 0) {
            arr.push(n);
        }
        if (n % 5 == 0) {
            arr.push(n);
        }
        n--;
    }
    arr.map(function(item) {
        sum += item;
    });
    return sum;
}
console.log(findSum(10));

// For building the encrypted string:
// Take every 2nd char from the string, then the other chars, that are not every 2nd char, and concat them as new String.
// Do this n times!

// Examples:

// "This is a test!", 1 -> "hsi  etTi sats!"
// "This is a test!", 2 -> "hsi  etTi sats!" -> "s eT ashi tist!"
function encrypt(text, n) {
    if (!text || !text.length || n <= 0) {
        return text;
    }
    while (n-- > 0) {
        var rightstr = text
            .split('')
            .filter(function(item, index) {
                return index % 2 == 0;
            })
            .join('');
        var leftstr = text
            .split('')
            .filter(function(item, index) {
                return index % 2 == 1;
            })
            .join('');

        text = leftstr + rightstr;
    }
    return text;
}
var encrypt0 = encrypt('This is a test!', 0);
var encrypt1 = encrypt('This is a test!', 1);
var encrypt2 = encrypt('This is a test!', 2);
var encrypt3 = encrypt('This is a test!', 3);

function decrypt(encryptedText, n) {
    if (!encryptedText || !encryptedText.length || n <= 0) {
        return encryptedText;
    }
    var first = encryptedText.slice(0, encryptedText.length / 2);
    var second = encryptedText.slice(encryptedText.length / 2);
    var result = new Array(encryptedText.length);

    while (n--) {
        var j = 0;
        for (var i = 1; i < result.length; i += 2) {
            result[i] = encryptedText[j++];
        }
        for (var i = 0; i < result.length; i += 2) {
            result[i] = encryptedText[j++];
        }
        encryptedText = result.join('');
    }

    return encryptedText;
}
var result0 = decrypt(encrypt0, 0);
var result1 = decrypt(encrypt1, 1);

